<template>
    <div class="set-containter">
        <el-tabs type="border-card" v-if="loadFlag">
            <el-tab-pane label="1商家信息设置">
				<el-merchant 
				@submitMerchant="setMerInfo" 
				:merchant-info="merchantInfo" ref="mer" @setMerchantService="setMerchantService"></el-merchant>
			</el-tab-pane>
            <el-tab-pane label="2服务配置" :disabled="c_filter_info">
				<el-ser-config @subSerInfo="subSerInfo" :merchant-info="merchantInfo" ref="ser"  :merchant-id="merchantId" :cart-id-list="carts"></el-ser-config>
			</el-tab-pane>
            <el-tab-pane label="3小程序配置" :disabled="c_filter_info">
				<el-wx-config @subWxInfo="subWxInfo" :merchant-info="merchantInfo" ref="wer"></el-wx-config>
			</el-tab-pane>
			<el-row class="box-flex">
				<el-button type="primary" class="pri-btn" @click="save" :disabled="c_filter_info">保存</el-button>
			</el-row>
        </el-tabs>
		
    </div>
</template>

<script>
	import elMerchant from './child/elMerchant.vue'
	import elSerConfig from './child/elSerConfig.vue'
	import elWxConfig from './child/elWxConfig.vue'
	import {
		SUCCESS_REQUEST_STATUS
	} from '@/utils/config.js'
	export default {
		props: {
			// 商家ID
			merchantId: {
				type: [String, Number],
				default: ''
			}
		},
		data() {
			return {
				ser: true,
				wxInfo: null,
				merInfo: null,
				serInfo: null,
				loadFlag: false,
				catIds: [],
				carts: null,
				c_filter_info: true,
				merchantInfo: {},
				
			}
		},
		created() {
			if(this.merchantId) {
				this._initMerchantInfo()
				return
			}
			this.loadFlag = true
		},
		methods: {
			setMerchantService(val) {
				this.c_filter_info = val
			},
			_initMerchantInfo() {
				this.$get({
					url: `merchant/${this.merchantId}`,
					callback: res => {
						console.log(res)
						if(res.status === 200) {
							this.carts = res.data.config.category
							this.merchantInfo = Object.assign({}, res.data.weixinConfig, res.data.config,res.data)
							this.loadFlag = true
						}
					}
				})
				return
// 				this.$ajax({
// 					api: 'merchant/info',
// 					params: {
// 						merchantId: this.merchantId
// 					}
// 				}).then(res => {
// 					console.log(res)
// 					if(res.status === SUCCESS_REQUEST_STATUS) {
// 						console.log(res.data.catIds)
// 						this.carts = res.data.catIds
// 						this.merchantInfo = Object.assign({}, res.data.merchant, res.data.wechatMerchant)
// 						console.log(this.merchantInfo)
// 						this.loadFlag = true
// 					}
// 				})
			},
			// 服务信息
			subSerInfo(info ,carts) {
				this.serInfo = info
				this.carts = carts
				
			},
			// 微信信息
			subWxInfo(info) {
				this.wxInfo = info
			},
			save(info) {
				this.$refs.mer.submitMerchant()
				this.$refs.ser.subSerInfo()
				this.$refs.wer.subWxInfo()
				if(this.merInfo){
					this.saveInfo()
				}
			},
			setMerInfo(merInfo) {
				this.merInfo = merInfo
			},
			handleCartIdFn(attr){
				let arr = []
				attr.forEach((item) => {
					arr.push({categoryId: item})
				})
				return arr
			},
			saveInfo() {
				var params = new URLSearchParams()
				let par = {
					number: this.merInfo.number,
					account: this.merInfo.account,
					type: parseInt(this.merInfo.type),
					merchantName: this.merInfo.merchantName,
					companyName: this.merInfo.merchantName,
					cellPhoneNumber: this.merInfo.cellPhoneNumber,
					address: this.merInfo.address,
					contactor: this.merInfo.contactor,
					contactorPhoneNumber: this.merInfo.contactorPhoneNumber,
					contractCode: this.merInfo.contractCode,
					remark: this.merInfo.remark,
					id: this.merchantId ? this.merchantId : ''
				}
				
				if(this.serInfo){
					par.config = {
						indexStyle: this.serInfo ? parseInt(this.serInfo.indexStyle) :1,
						miniProgram: this.serInfo ? parseInt(this.serInfo.miniProgram ? 1 : 0) : 0,
						weixinWeb: this.serInfo ? parseInt(this.serInfo.weixinWeb ? 1 : 0) : 0,
						onlineStore: this.serInfo ? parseInt(this.serInfo.onlineStore? 1 : 0) : 0,
						memberLevelPrice: this.serInfo ? parseInt(this.serInfo.memberLevelPrice ? 1 :0) : 0,
						paidAccount: this.serInfo ? parseInt(this.serInfo.paidAccount ? 1 : 0) : 0,
						oemSearch: this.serInfo ? parseInt(this.serInfo.oemSearch ? 1 :0) : 0,
						commodityModel: this.serInfo ?  parseInt(this.serInfo.commodityModel ? 1 :0): 0,
						memberRegistry: this.serInfo ?  parseInt(this.serInfo.memberRegistry ? 1 : 0) : 0,
						avoidVisitor: this.serInfo ? (this.serInfo.avoidVisitor ? 1 :0) : 0,
						vinSearch: this.serInfo ?parseInt( this.serInfo.vinSearch ? 1 : 0) : 0,
						vinSearchDailyCount: this.serInfo ? parseInt(this.serInfo.vinSearchDailyCount) : 0,
						vinSearchFee:  this.serInfo ? parseInt(this.serInfo.vinSearchFee ? 1 :0) : 0,
						vinSearchCount: this.serInfo ? parseFloat(this.serInfo.vinSearchCount) : 0,
						maintain: this.serInfo ? parseInt(this.serInfo.maintain ? 1 :0) : 0,
						customer: this.serInfo ? parseInt(this.serInfo.customer ? 1 :0): 0,
						expireTime: '2018-09-17 16:56:34',
						category:this.handleCartIdFn(this.carts),
						merchantId: this.merchantId ? this.merchantId : ''
					}
				}
				if(this.wxInfo){
					par.weixinConfig = {
						publisherAppId: this.wxInfo ? this.wxInfo.publisherAppId : '',
						publisherAppSecret : this.wxInfo ? this.wxInfo.publisherAppSecret  : '',
						miniProgramAppId : this.wxInfo ? this.wxInfo.miniProgramAppId  : '',
						miniProgramAppSecret: this.wxInfo ? this.wxInfo.miniProgramAppSecret : '',
						merchantPlatformId: this.wxInfo ? this.wxInfo.merchantPlatformId : '',
						merchantPlatformApiKey: this.wxInfo ? this.wxInfo.merchantPlatformApiKey : '',
						merchantId: this.merchantId ? this.merchantId : ''
					}
					
				}
				
				if(!this.serInfo.vinSearchCount){
					this.$message({
						type:'error',
						message:'请填写默认查询次数'
					})
					return
				}
// 				for(var key in par){
// 					params.append(key,par[key]);
// 				}
// 				if(this.carts) {
// 					this.carts.forEach((item) => {
// 						 params.append('catIds',item);
// 					})
// 				}
				console.log(params)
				console.log('9999999999')
				if(this.merchantId){
					this.$put({
						url: 'merchant/?accountId=10',
						data: par,
						callback: res => {
							console.log(res)
							if(res.status === 200) {
								this.$message({
									message:'编辑成功',
									type:'success'
								})
								this.$emit('saveMerchantInfo')
							} else{
								this.$message({
									message:res.msg,
									type:'error'
								})
							}
						}
					})
					return
				}
				this.$post({
					url: 'merchant/',
					data: par,
					callback: res => {
						console.log(res)
						if(res.status === 200) {
							this.$message({
								message:'操作成功',
								type:'success'
							})
							this.$emit('saveMerchantInfo')
						} else{
							this.$message({
								message:res.msg,
								type:'error'
							})
						}
						
					}
				})
// 				return
// 				this.$http.post('http://118.31.19.251:9000/merchant/', params).then((res) => {
// 					console.log(res)
// 					if(res.status === SUCCESS_REQUEST_STATUS) {
// 						if(res.data.status == 404) {
// 							this.$message({
// 								message:res.data.message,
// 								type:'error'
// 							})
// 							return
// 						}
// 						this.$message({
// 							message:'操作成功',
// 							type:'success'
// 						})
// 						this.$emit('saveMerchantInfo')
// 					}
// 				}).catch(err => {
// 					console.log(err)
// 				})
			},
			
		},
		components: {
			elMerchant,
			elSerConfig,
			elWxConfig
		}
	}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .set-containter {
        display: flex;
        flex: 1;
		height: 100%;
		box-sizing: border-box;
        flex-direction: column;
		.box-flex{
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;
		}
    }
</style>
